
#include "shading.fx"
#include "samplers.fx"
#include "fog.fx"

struct VS_IN
{
	float3 position : POSITION;
	float3 normal	: NORMAL;
	float3 tangent : TANGENT;
	float2 tex		: TEX_COORDS;
};

struct VS_OUT
{
	float4 position : SV_POSITION;
	float4 ws_pos	: WS_POSITION;
	float3 normal	: NORMAL;
	float2 tex		: TEX_COORDS;
};


float4x4 m_World;
float4x4 m_WorldViewProj;
Texture2D texSand;


VS_OUT VS(VS_IN vin)
{
	VS_OUT vout;

	vout.position = mul(float4(vin.position, 1.0f), m_WorldViewProj);
	vout.ws_pos = mul(float4(vin.position, 1.0f), m_World);
	vout.normal = mul(float4(vin.normal, 0.0f), m_World).xyz;
	vout.tex =	  vin.tex;

	return vout;
}




float4 PS(VS_OUT pin) : SV_TARGET
{
	
	// color = shade() : diffuse+specular
	float4 color = float4(shade(pin.ws_pos.xyz, normalize(pin.normal), 0), 1.0f);
	
	float4 ambient = float4(0.1f, 0.1f, 0.1f, 0.0f);

	// color	= color	   * tex_diffuse
	// ambient	= ambient  * tex_diffuse
	if (mat_params.vIsTextured.x)
	{
		float4 tex_diff = tex_diffuse.Sample(s_linear, pin.tex);
		float4 colorSand = texSand.Sample(s_linear, pin.tex);

		tex_diff = lerp(colorSand, tex_diff, (pin.ws_pos.y - 1.3f) / (1.85f-1.3f));

		color *= tex_diff;
		ambient *= tex_diff;
	}

	// set alpha in w
	color = float4(color.xyz, mat_params.diffuse.w);

	

	// color = color + ambient
	color += ambient;


	color = computeFog(color, pin.ws_pos.xyz, pin.ws_pos.y, 3.0f, pEye);

	return color;
}





RasterizerState rs_state
{
	CullMode = Back;
};

technique10 Island 
{
    pass P0 
	{
        SetVertexShader( CompileShader( vs_4_0, VS() ) );
		SetGeometryShader( NULL);
        SetPixelShader( CompileShader( ps_4_0, PS() ) );

		SetRasterizerState(rs_state);
	}
}
